import sqlite3
import os

DB_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "train.db")

def get_db_connection():
    conn = sqlite3.connect(DB_FILE)
    conn.row_factory = sqlite3.Row
    return conn

def update_payment_amount():
    conn = None
    try:
        conn = get_db_connection()
        cursor = conn.cursor()
        cursor.execute("UPDATE enroll SET payment_amount = 0.01")
        conn.commit()
        print("Successfully updated payment_amount to 0.01 for all entries in enroll table.")
    except sqlite3.Error as e:
        print(f"Database error: {e}")
        if conn:
            conn.rollback()
    finally:
        if conn:
            conn.close()

def update_transaction_id(entry_id: int, transaction_id: str):
    conn = None
    try:
        conn = get_db_connection()
        cursor = conn.cursor()
        cursor.execute("UPDATE enroll SET transaction_id = ? WHERE id = ?", (transaction_id, entry_id))
        conn.commit()
        print(f"Successfully updated transaction_id for entry_id {entry_id} to {transaction_id}.")
    except sqlite3.Error as e:
        print(f"Database error: {e}")
        if conn:
            conn.rollback()
    finally:
        if conn:
            conn.close()

def update_status(entry_id: int, status: str):
    conn = None
    try:
        conn = get_db_connection()
        cursor = conn.cursor()
        cursor.execute("UPDATE enroll SET status = ? WHERE id = ?", (status, entry_id))
        conn.commit()
        print(f"Successfully updated status for entry_id {entry_id} to {status}.")
    except sqlite3.Error as e:
        print(f"Database error: {e}")
        if conn:
            conn.rollback()
    finally:
        if conn:
            conn.close()

if __name__ == "__main__":
    # update_payment_amount() # Commented out as per previous task
    update_transaction_id(1, "4200002723202508181305024260")
    # 示例：更新 id 为 1 的记录的状态为 'completed'
    update_status(1, "paid")
    update_status(2, "paid")
